<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>检查一个偏移位置是否存在</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="class.arrayaccess.html">« ArrayAccess</a></li>
      <li style="float: right;"><a href="arrayaccess.offsetget.html">ArrayAccess::offsetGet »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.arrayaccess.html">ArrayAccess</a></li>
    <li>检查一个偏移位置是否存在</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="arrayaccess.offsetexists" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">ArrayAccess::offsetExists</h1>
  <p class="verinfo">(PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">ArrayAccess::offsetExists</span> &mdash; <span class="dc-title">检查一个偏移位置是否存在</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-arrayaccess.offsetexists-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>ArrayAccess::offsetExists</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$offset</code></span>): <span class="type">bool</span></div>

  <p class="para rdfs-comment">
   检查一个偏移位置是否存在。
  </p>
  <p class="para">
   对一个实现了 <span class="classname"><a href="class.arrayaccess.html" class="classname">ArrayAccess</a></span> 接口的对象使用 <span class="function"><a href="function.isset.html" class="function">isset()</a></span> 或 <span class="function"><a href="function.empty.html" class="function">empty()</a></span> 时，此方法将执行。
  </p>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
   当使用 <span class="function"><a href="function.empty.html" class="function">empty()</a></span> 并且仅当 <span class="function"><strong>ArrayAccess::offsetExists()</strong></span> 返回 <strong><code>true</code></strong> 时，<span class="function"><a href="arrayaccess.offsetget.html" class="function">ArrayAccess::offsetGet()</a></span> 将被调用以检查是为否空。
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 parameters" id="refsect1-arrayaccess.offsetexists-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    <dt>
<code class="parameter">offset</code></dt>

     <dd>

      <p class="para">
       需要检查的偏移位置。
      </p>
     </dd>

    
   </dl>

  </p>

 </div>


 <div class="refsect1 returnvalues" id="refsect1-arrayaccess.offsetexists-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code>true</code></strong>， 或者在失败时返回 <strong><code>false</code></strong>。
  </p>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    如果一个非布尔型返回值被返回，将被转换为 <span class="type">bool</span> 。
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 examples" id="refsect1-arrayaccess.offsetexists-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-391">
    <p><strong>示例 #1 <span class="function"><strong>ArrayAccess::offsetExists()</strong></span> 范例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">obj&nbsp;</span><span style="color: #007700">implements&nbsp;</span><span style="color: #0000BB">arrayaccess&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">offsetSet</span><span style="color: #007700">(</span><span style="color: #0000BB">$offset</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">):&nbsp;</span><span style="color: #0000BB">void&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">__METHOD__</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">offsetExists</span><span style="color: #007700">(</span><span style="color: #0000BB">$var</span><span style="color: #007700">):&nbsp;</span><span style="color: #0000BB">bool&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">__METHOD__</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">$var&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #DD0000">"foobar"</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">offsetUnset</span><span style="color: #007700">(</span><span style="color: #0000BB">$var</span><span style="color: #007700">):&nbsp;</span><span style="color: #0000BB">void&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">__METHOD__</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">#[ReturnTypeWillChange]<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">offsetGet</span><span style="color: #007700">(</span><span style="color: #0000BB">$var</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">__METHOD__</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #DD0000">"value"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">$obj&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">obj</span><span style="color: #007700">;<br /><br />echo&nbsp;</span><span style="color: #DD0000">"Runs&nbsp;obj::offsetExists()\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(isset(</span><span style="color: #0000BB">$obj</span><span style="color: #007700">[</span><span style="color: #DD0000">"foobar"</span><span style="color: #007700">]));<br /><br />echo&nbsp;</span><span style="color: #DD0000">"\nRuns&nbsp;obj::offsetExists()&nbsp;and&nbsp;obj::offsetGet()\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(empty(</span><span style="color: #0000BB">$obj</span><span style="color: #007700">[</span><span style="color: #DD0000">"foobar"</span><span style="color: #007700">]));<br /><br />echo&nbsp;</span><span style="color: #DD0000">"\nRuns&nbsp;obj::offsetExists(),&nbsp;*not*&nbsp;obj:offsetGet()&nbsp;as&nbsp;there&nbsp;is&nbsp;nothing&nbsp;to&nbsp;get\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(empty(</span><span style="color: #0000BB">$obj</span><span style="color: #007700">[</span><span style="color: #DD0000">"foobaz"</span><span style="color: #007700">]));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Runs obj::offsetExists()
string(17) &quot;obj::offsetExists&quot;
bool(true)

Runs obj::offsetExists() and obj::offsetGet()
string(17) &quot;obj::offsetExists&quot;
string(14) &quot;obj::offsetGet&quot;
bool(false)

Runs obj::offsetExists(), *not* obj:offsetGet() as there is nothing to get
string(17) &quot;obj::offsetExists&quot;
bool(true)
</pre></div>
    </div>
   </div>
  </p>
 </div>





</div></div></div></body></html>